package com.gionee.navil.lockscreen;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class GnNavilIndicator2 extends View {
	private final static String TAG = "GnNavilIndicator2";
	private static int sDisplayW;
	private static int sIndicatorW = 520;
	private static int sIndicatorH = 12;

	private int mCurPage;
	private int mCountPage;
	private int mCurPosX;
	private int mSingleIndicatorW;
	private int mDx = 0;

	private int mMaxScreenCenter;
	private int mMinScreenCenter;
	private int mInitScreenCenter;

	private Drawable mIndicatorDw;
	private Drawable mIndicatorBg;

	private ValueAnimator mAnimator;

	public GnNavilIndicator2(Context context) {
		this(context, null);
	}

	public GnNavilIndicator2(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public GnNavilIndicator2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Resources rs = context.getResources();
		sDisplayW = rs.getDisplayMetrics().widthPixels;
		mIndicatorDw = rs.getDrawable(R.drawable.zzzzz_gn_navil_indicator);
		mIndicatorBg = rs.getDrawable(R.drawable.zzzzz_gn_navil_indicator_bg);
		mAnimator = ValueAnimator.ofInt(255, 0);
		mAnimator.setInterpolator(new TimeInterpolator() {

			@Override
			public float getInterpolation(float input) {
				return (float) (Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
			}
		});
		mAnimator.setDuration(1000);
		mAnimator.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator arg0) {
				GnLog.d(TAG, "arg0.getAnimatedValue=" + arg0.getAnimatedValue());
				mIndicatorDw.setAlpha((Integer) arg0.getAnimatedValue());
				invalidate();
			}
		});

	}

	public void setCurPage(int curPage) {
		GnLog.d(TAG, "setCurPage curPage=" + curPage);
		mCurPage = curPage;
		requestLayout();
		invalidate();
	}

	public void setCountPage(int countPage) {
		GnLog.d(TAG, "setCountPage countPage=" + countPage);
		mCountPage = countPage;
	}

	public void move(int screenCenter) {
		// GnLog.d(TAG, "move screenCenter=" + screenCenter);
		mAnimator.cancel();
		mIndicatorDw.setAlpha(255);
		if (screenCenter < mMinScreenCenter) {
			screenCenter = mMinScreenCenter;
		}

		if (screenCenter > mMaxScreenCenter) {
			screenCenter = mMaxScreenCenter;
		}

		int dx = screenCenter - mInitScreenCenter;
		// GnLog.d(TAG, "move dx=" + dx);
		mDx = (dx * sIndicatorW) / (mCountPage * sDisplayW);
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		mIndicatorBg.draw(canvas);

		mIndicatorDw.setBounds(mCurPosX + mDx, 0, mCurPosX + mDx
				+ mSingleIndicatorW, sIndicatorH);
		mIndicatorDw.draw(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(sDisplayW, sIndicatorH);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		if (mCountPage > 0 && mCurPage < mCountPage) {
			int w = sIndicatorW / mCountPage;
			int bgW = mCountPage * w;
			mDx = 0;
			int indicatorLeftX = (sDisplayW - bgW) / 2;
			mIndicatorBg.setBounds(indicatorLeftX, 0, indicatorLeftX + bgW,
					sIndicatorH);
			mSingleIndicatorW = w;
			mCurPosX = mCurPage * w + indicatorLeftX;
			mInitScreenCenter = sDisplayW / 2 + mCurPage * sDisplayW;
			mMinScreenCenter = sDisplayW / 2;
			mMaxScreenCenter = sDisplayW / 2 + (mCountPage - 1) * sDisplayW;
		}
	}

	public void onPageSwitch(int newIndex) {
		mAnimator.start();
	}

}
